<?php

class CpservicesController extends Core_Controller_Action 
{
	private $_api;
	
	public function init() {	
		$this->_api = new Services();
	}
	public function indexAction(){
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$pagerLayout = $this->_api->getListWithPager( $page, $this->view->baseUrl() );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		
	}
	public function listAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$status = intval( $this->_request->getParam( 'status', 0 ) );
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$this->view->status = $status;
		$this->view->prod_id = $id;
		
		$pagerLayout = $this->_api->getListWithPagerStatus( $page, $this->view->baseUrl(), $id, $status );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
	
	}
	public function addAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$pid = intval( $this->_request->getParam( 'pid', 0 ) );
		$status = intval( $this->_request->getParam( 'status', 0 ) );
		
		if($id)
			$this->view->item = $this->_api->getItem( $id )->getFirst()->toArray();
		else
			$this->_redirect('cpservices');
			
		if($this->_request->isPost())
		{
			$api = $this->_api->getItem( $id )->getFirst();
			$api->data 	= $this->_getParam('data');
			if($this->_getParam('comment')!='')
				$api->comment = $api->comment.date("Y-m-d H:i:s"). '<br /> '.$this->userData->email.' napisał: <br />'.$this->_getParam('comment')."<br /><br /> ";
			
			$api->save();
			$this->_redirect('cpservices/list/id/'.$pid.'/status/'.$status);
		}
		
	}
	public function statusAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$pid = intval( $this->_request->getParam( 'pid', 0 ) );
		$status = intval( $this->_request->getParam( 'status', 0 ) );
		
		$api = $this->_api->getItem( $id )->getFirst();
		$api->status = $api->status + 1;
		$api->save();
		
		$this->_redirect('cpservices/list/id/'.$pid.'/status/'.$status);		
	}
}
	